﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Common.Forms;


namespace RockCompany.Master.View
{
    public partial class SC03 : ParentFrm
    {
        private Translator trans;
        private string sUserName; // login user
        private UserAuthenticate uAuth;

        //Model
        private SC03FormModel modelSC03;

        // Store origional username
        private string orginalUName;

        public SC03()
        {
            // Initial forms' components
            InitializeComponent();
            trans = new Translator();
            // this.trans = translator;
            trans.controlTranslate(this);
        }

        public SC03(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            // Initial forms' components
            InitializeComponent();
            //trans = new Translator();
            this.trans = translator;
            trans.controlTranslate(this);
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUName);
            sUserName = sUName;
        }

        // Load data for combobox
        private void SC03_Load(object sender, EventArgs e)
        {
            if (modelSC03 == null)
            {
                modelSC03 = new SC03FormModel();
            }
            DataTable groupsDT = modelSC03.GetGroups();

            CommonModel.bindCombo(cbGroupName, SC03Const.TEN_NHOM_QLY, SC03Const.MS_NHOM_QLY, groupsDT);
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            if (modelSC03 == null)
            {
                modelSC03 = new SC03FormModel();
            }
            DataTable resultDT = modelSC03.GetUsers(createUserDTO(SC03Const.F_SEARCH_CD));

            if (CommonModel.existData(resultDT))
            {
                this.dgvUserList.DataSource = resultDT;
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (validate(SC03Const.F_ADD_CD) && InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                                         , trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                if (modelSC03.InsertUser(createUserDTO(SC03Const.F_ADD_CD), sUserName))
                {
                    btSearch_Click(sender, e);
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
                else
                {
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }

        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            List<string> checkedList = CommonModel.getCheckedList(colChk.Name, colUsername.Name, dgvUserList);
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                                         , trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (checkedList.Count > 0)
                {
                    modelSC03.DeleteUsers(checkedList);
                    btSearch_Click(sender, e);
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.RemoveComplete),
                        trans.getMessageCaption(CommonConstantIF.InforBoxCap));

                }
                else
                {
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SELECTED_ROW),
                        trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void btModify_Click(object sender, EventArgs e)
        {
            if (validate(SC03Const.F_MODIFY_CD) && InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                                         , trans.getMessageCaption(CommonConstantIF.CONFIRM_MODIFY)))
            {
                if (modelSC03.ModifyUser(createUserDTO(SC03Const.F_MODIFY_CD), sUserName))
                {
                    btSearch_Click(sender, e);
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.UpdateComplete),
                        trans.getMessageCaption(CommonConstantIF.InforBoxCap));

                }
                else
                {
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.UpdateError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private UserDTO createUserDTO(int funcCd)
        {
            UserDTO userDTO = new UserDTO();

            if (funcCd == SC03Const.F_SEARCH_CD)
            {
                userDTO.username = this.tbUsernameSrc.Text.Trim();
                userDTO.groupName = this.tbGroupNameSrc.Text.Trim();
            }
            else
            {
                if (funcCd == SC03Const.F_ADD_CD)
                {
                    userDTO.username = this.tbUsername.Text;
                }
                else if
                    (funcCd == SC03Const.F_MODIFY_CD)
                {
                    userDTO.username = this.orginalUName;
                }
                userDTO.password = this.tbPassword.Text;
                userDTO.groupName = this.cbGroupName.Text;
                userDTO.groupCode = this.cbGroupName.SelectedValue.ToString();

                userDTO.fullname = this.tbFullname.Text.Trim();
                userDTO.employeeNo = this.tbEmployeeNo.Text.Trim();
            }

            return userDTO;
        }

        private bool validate(int funcCd)
        {
            string un = tbUsername.Text, pw = tbPassword.Text;
            // Check mandatory field
            if (UserValidator.IsEmpty(un)
                || UserValidator.IsEmpty(pw)
                || UserValidator.IsEmpty(tbFullname.Text)
                || UserValidator.IsEmpty(cbGroupName.Text))
            {
                InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                return false;
            }

            // Check username
            UserValidator validator = new UserValidator();
            if (!UserValidator.IsValidUserName(un))
            {
                InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SC03_USERNAME_INVALID), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                return false;
            }

            if (funcCd == SC03Const.F_ADD_CD && validator.ExistsUserName(un))//check username existance
            {
                InforDialog.showMessageBox(trans.getMessageCaption(CommonConstantIF.ErrorBoxCap), trans.getMessageCaption(CommonConstantIF.SC03_USERNAME_EXISTS));
                return false;
            }
            return true;
        }

        private void dgvUserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0 && e.RowIndex > -1) // Ignore a click event on first column
            {
                DataGridViewRow dgvRow = dgvUserList.Rows[e.RowIndex];
                tbEmployeeNo.Text = dgvRow.Cells[colEmployeeNo.Name].EditedFormattedValue.ToString();
                tbFullname.Text = dgvRow.Cells[colFullname.Name].EditedFormattedValue.ToString();
                tbUsername.Text = dgvRow.Cells[colUsername.Name].EditedFormattedValue.ToString();

                this.orginalUName = tbUsername.Text;
                btModify.Enabled = this.orginalUName.Equals(this.sUserName); // enable button Modify if and only login user modified their info

                int selIndex = cbGroupName.FindStringExact((dgvRow.Cells[colGroupName.Name].Value.ToString()));
                cbGroupName.SelectedIndex = selIndex;
            }
        }

        private void SC03_SizeChanged(object sender, EventArgs e)
        {
            //Util.reLocate(this);
        }
    }
}